/**
 * Created by 37500 on 2017/8/24.
 */
const path = require('path');
const webpack = require('webpack');
const _ = require('lodash/object');

const ExtractTextPlugin = require("extract-text-webpack-plugin");

const extractSass = new ExtractTextPlugin({
    filename: "/css/[name].css",
    disable: process.env.NODE_ENV === "development"
});

module.exports = {
    entry : {
        app: './app.js'
    },
    output: {
        filename : '[name].bundle.js',
        path : path.resolve(__dirname, 'dist'),
        publicPath : '/dist/'
    },
    module: {
        rules : [
            {
                test: /\.(js|jsx)/,
                exclude: /node_modules/,
                use : {
                    loader : 'babel-loader',
                    options: {
                        presets: ['env', 'stage-0', 'react'],
                        plugins: ['transform-runtime']
                    }
                }
            }, {
                test: /\.(png|jpg|gif|svg|eot|woff|ttf)$/,
                loader: 'file-loader',
                options: {
                    name: '[name].[ext]?[hash]'
                }
            }, {
                test: /\.scss$/,
                use: extractSass.extract({
                    use: [{
                        loader: "css-loader"
                    }, {
                        loader: "sass-loader"
                    }],
                    // 在开发环境使用 style-loader
                    fallback: "style-loader"
                })
            }
        ]
    },

    plugins: process.env.NODE_ENV === 'production' ? [
        new webpack.optimize.UglifyJsPlugin(),
        extractSass,
        new webpack.optimize.CommonsChunkPlugin({
            name: 'common',
            minChunks: Infinity
        })
    ] : [extractSass]
};